package com.ldk.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ldk.base.framework.util.SystemUtil;
import com.ldk.framework.entity.Oplog;
import com.ldk.framework.service.IOplogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ldk.admin.common.LigerUIGridResult;
import com.ldk.admin.common.ResponseResult;
import com.ldk.admin.constants.FORM;
import com.ldk.admin.constants.ViewMode;
import com.ldk.base.framework.common.CustomPageable;
import com.ldk.framework.entity.RechargeDiscount;
import com.ldk.framework.service.IRechargeDiscountService;

@Controller
@RequestMapping("/recharge/discount")
public class RechargeDiscountController extends BaseController {

	/**
	 * 路由地址
	 */
	private final String route = "recharge/discount";
	/**
	 * 模块名称
	 */
	private final String module = "充值优惠折扣";
	
	@Autowired
	private IRechargeDiscountService rechargeDiscountService;

	@RequestMapping("/list")
	//public ModelAndView list(ModelMap map){
	public ModelAndView list(ModelMap map,HttpServletRequest request,
							 HttpServletResponse response){
		//session是否过期
		this.isExpired(request,response);
		if (this.getRoleId(request) == 6) {
			setLinks(map, "", "", module+"管理", "/"+route+"/list", "", "");
		} else {
			setLinks(map, "", "", module+"管理", "/"+route+"/list", "添加"+module, "/"+route+"/add");
		}

		map.addAttribute("route", route);
		map.addAttribute("module", module);

		//操作日志
		//this.insertOplog(request,module+"管理","","","查看",this.getAdminUserName(request).toString(), Long.parseLong(this.getAdminId(request).toString()),SystemUtil.getAllSeconds());

		if (this.getRoleId(request) == 6) {
			return new ModelAndView("/recharge/discount/list_sj");
		} else {
			return new ModelAndView("/recharge/discount/list");
		}
	}
	
	@ResponseBody
	@RequestMapping("/getList")
	public LigerUIGridResult<RechargeDiscount> getList(@RequestParam(value="page",required=false,defaultValue="1") int page, 
									           @RequestParam(value="pagesize",required=false,defaultValue="10") int pageSize,
									           HttpServletRequest request)
	{
		int limit = pageSize;
		int offset = (page -1) * pageSize;
		
		RechargeDiscount query = new RechargeDiscount();
		CustomPageable pageable = new CustomPageable(offset, limit);
		
		Page<RechargeDiscount> result = rechargeDiscountService.selectPageList("", query, pageable);
		
		LigerUIGridResult<RechargeDiscount> grid = new LigerUIGridResult<>();
		grid.Total = result.getTotalElements();
		grid.Rows = result.getContent();
		
		return grid;
	}
	
	@RequestMapping("/add")
	public ModelAndView add(ModelMap map,HttpServletRequest request,
							HttpServletResponse response){
		//session是否过期
		this.isExpired(request,response);
		setLinks(map, module+"管理", "/"+route+"/list", "添加"+module, "/"+route+"/add", "", "");
		setForm(map, "/"+route+"/insert", null);
		map.addAttribute("module", module);
		
		RechargeDiscount model = new RechargeDiscount();
		
		return new ModelAndView("/recharge/discount/info","model",model);
	}
	
	@RequestMapping("/edit/{discountId}")
	public ModelAndView edit(@PathVariable int discountId, ModelMap map,HttpServletRequest request,
							 HttpServletResponse response){
		//session是否过期
		this.isExpired(request,response);
		RechargeDiscount query = new RechargeDiscount();
		query.setDiscountId(discountId);
		
		RechargeDiscount model = rechargeDiscountService.selectByKey("", query);
		//如果不存在返回列表
		if(model == null){
			return new ModelAndView("redirect:/"+route+"/list");
		}
		setLinks(map, module+"管理", "/"+route+"/list", "编辑"+module, "/"+route+"/edit/"+discountId, "", "");
		setForm(map, "/"+route+"/update", null);
		map.addAttribute("module", module);
		
		return new ModelAndView("/recharge/discount/info","model",model);
	}
	
	@RequestMapping(value="/insert",method=RequestMethod.POST)
	public ModelAndView insert(@ModelAttribute("model") RechargeDiscount model, RedirectAttributes redAttr, ModelMap map,
							   HttpServletRequest request,
							   HttpServletResponse response)
	{
		//session是否过期
		this.isExpired(request,response);
		ResponseResult rst = new ResponseResult();
		try{
			if(model.getEnabled() == null){
				model.setEnabled(true);
			}
			if(model.getRemark() == null){
				model.setRemark("");
			}

			//操作日志
			this.insertOplog(request,module,"","","添加:"+model.getDiscountId(),this.getAdminUserName(request).toString(),
					Long.parseLong(this.getAdminId(request).toString()),SystemUtil.getAllSeconds());

		    int row  = rechargeDiscountService.insert("", model);
		    if(row > 0){
		    	rst.AddOk("添加"+module+"成功");
		    }
		    else{
		    	rst.AddError("添加"+module+"失败");
		    }
		}
		catch(Exception e){
			rst.AddError("添加"+module+"失败");
		}
		
		redAttr.addFlashAttribute(FORM.FROM_RESULT, rst);
    	
		return new ModelAndView("redirect:/"+route+"/add");
	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public ModelAndView update(@ModelAttribute("model") RechargeDiscount model,RedirectAttributes redAttr, ModelMap map,
							   HttpServletRequest request,
							   HttpServletResponse response)
	{
		//session是否过期
		this.isExpired(request,response);
		ResponseResult rst = new ResponseResult();
		redAttr.addAttribute("discountId", model.getDiscountId());
		redAttr.addFlashAttribute(FORM.FROM_RESULT, rst);
		try{
			if(model.getEnabled() == null){
				model.setEnabled(true);
			}
			if(model.getRemark() == null){
				model.setRemark("");
			}
			
		    int row  = rechargeDiscountService.updateByKey("", model);
		    if(row > 0){
				//操作日志
				this.insertOplog(request,module,"","","修改:"+model.getDiscountId(),this.getAdminUserName(request).toString(),
						Long.parseLong(this.getAdminId(request).toString()),SystemUtil.getAllSeconds());
		    	rst.AddOk("编辑"+module+"成功");
		    }
		    else{
		    	rst.AddError("编辑"+module+"失败");
		    }
		}
		catch(Exception e){
			rst.AddError("编辑"+module+"失败");
		}
		
		return new ModelAndView("redirect:/"+route+"/edit/{discountId}");
	}
	
	/**
	 * 浏览
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping("/view/{discountId}")
	public ModelAndView view(@PathVariable int discountId, ModelMap map,HttpServletRequest request,
							 HttpServletResponse response){
		//session是否过期
		this.isExpired(request,response);
		setViewMode(map, ViewMode.View);
		RechargeDiscount query = new RechargeDiscount();
		query.setDiscountId(discountId);
		RechargeDiscount model = rechargeDiscountService.selectByKey("", query);
		map.addAttribute("module", module);
		
		return new ModelAndView("/recharge/discount/info","model", model);
	}
	
	@ResponseBody
	@RequestMapping("/remove/{discountId}")
	public ResponseResult remove(@PathVariable int discountId, HttpServletRequest request){
		ResponseResult rst = new ResponseResult();
		
		if(discountId < 1)
		{
			rst.AddError("删除"+module+"错误");
		}
		Boolean succeed = false;
		try{
			RechargeDiscount query = new RechargeDiscount();
			query.setDiscountId(discountId);
			RechargeDiscount rechargeDiscount = rechargeDiscountService.selectByKey("", query);
			//操作日志
			this.insertOplog(request,module,"","","删除:"+rechargeDiscount.getDiscountId(),this.getAdminUserName(request).toString(),
					Long.parseLong(this.getAdminId(request).toString()),SystemUtil.getAllSeconds());

			int row  = rechargeDiscountService.deleteByKey("", query);
			succeed = row > 0;
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
		if(succeed){
			rst.AddOk("删除"+module+"成功");
		}
		else{
			rst.AddError("删除"+module+"失败");
		}
		
		return rst;
	}
}
